﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Zarion_Quest.Base
{
    class Player : AnimatedObject2D
    {
        private bool isScreenBound;
        public bool IsScreenBound {
            get { return isScreenBound; }
            set { isScreenBound = value; }
        }

        private ShotManager shotManager;

        public Player(Texture2D texture, int columns, int rows)
            : base(texture, columns, rows)
        {
            shotManager = new ShotManager();
        }

        /// <summary>
        /// Método para deslocar o inimigo recebendo um Vector2 com a 
        /// </summary>
        /// <param name="quantidade">Quantidade de pixeles em X e Y que deve ser deslocado</param>
        public void move(Vector2 quantidade)
        {
            if(isScreenBound)
                screenBoundMove(quantidade);
            Position = Position + quantidade;
        }

        /// <summary>
        /// Método para deslocar o inimigo pelo eixo X
        /// </summary>
        /// <param name="X">Quantidade de pixles que deve ser deslocado</param>
        public void moveOnX(float X)
        {
            move(new Vector2(X, 0));
        }

        /// <summary>
        /// Método para deslocar o inimigo pelo eixo Y
        /// </summary>
        /// <param name="Y">Quantidade de pixels</param>
        public void moveOnY(float Y)
        {
            move(new Vector2(0, Y));
        }

        private void screenBoundMove(Vector2 quantidade)
        {
            Vector2 finalPosition;

            finalPosition = Position + quantidade;

            if (Position.X + FrameWidth / 2 >= Game1.Self.Window.ClientBounds.Width)
            {
                finalPosition.X = Game1.Self.Window.ClientBounds.Width - FrameWidth / 2;
            }
            else if (Position.X - FrameWidth / 2 <= 0)
            {
                finalPosition.X = 0 + FrameWidth / 2;
            }


            if (Position.Y + FrameHeight / 2 >= Game1.Self.Window.ClientBounds.Height)
            {
                finalPosition.Y = Game1.Self.Window.ClientBounds.Height - FrameHeight / 2;
            }
            else if (Position.Y - FrameHeight / 2 <= 0)
            {
                finalPosition.Y = 0 + FrameHeight / 2;
            }

            Position = finalPosition;
        }

        public void shoot(BaseShot shot)
        {
            shotManager.addShot(shot);
        }

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
            shotManager.UpdateShots(gameTime);
            EnemyManager.UpdateEnemies(gameTime);
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            base.Draw(spriteBatch);
            shotManager.DrawShots(spriteBatch);
            EnemyManager.DrawEnemies(spriteBatch);
        }
    }
}
